package com.chartdemo.data;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

public class DataFromJson
{
	private JSONObject jsonObj;
	private String[] pieName;
	private double[] mValue;
	
	public DataFromJson()
	{
	}
	
	/**
	 * 解析请求到的json数据 timeStyle day-0 year-1 month-2 hour-3
	 * 
	 * @param jsonStr 得到的json数据
	 * @param timerange 时间区间1-12day(hours)
	 * @return
	 */
	public double[] dealJsonData(String jsonStr, String timerange)
	{
		String[] mData = null;
		try
		{
			switch (TimeStyle.TIMESTYLE)
			{
				case 0:
					jsonObj = new JSONObject(jsonStr);
					String day = null;
					if ("1to12".equals(timerange))
					{
						day = jsonObj.getString("1to12");
					}
					else if ("13to24".equals(timerange))
					{
						day = jsonObj.getString("13to24");
					}
					mData = day.split("\\.");
					break;
				case 1:
					jsonObj = new JSONObject(jsonStr);
					String year = jsonObj.getString("year");
					mData = year.split("\\.");
					break;
				case 2:
					jsonObj = new JSONObject(jsonStr);
					String month = null;
					if ("1to12".equals(timerange))
					{
						month = jsonObj.getString("1to12");
					}
					else if ("13to24".equals(timerange))
					{
						month = jsonObj.getString("13to24");
					}
					else if ("25to30".equals(timerange))
					{
						month = jsonObj.getString("25to30");
					}
					mData = month.split("\\.");
					break;
				case 3:
					jsonObj = new JSONObject(jsonStr);
					String hour = jsonObj.getString("hour");
					mData = hour.split("\\.");
					break;
			}
		}
		catch (JSONException e)
		{
			mData = null;
			e.printStackTrace();
		}
		return this.transfer(mData);
	}
	
	/**
	 * 把String[]转换为double[]
	 * 
	 * @param mData
	 * @return
	 */
	public double[] transfer(String[] mData)
	{
		double[] mdouble = new double[mData.length];
		for (int i = 0; i < mData.length; i++)
		{
			mdouble[i] = Double.valueOf(mData[i]).doubleValue();
		}
		return mdouble;
	}
	
	/**
	 * 将饼图数据解析成name和Value数组
	 * 
	 * @param jsonStr 获取的Json数据
	 */
	public void pieData(String jsonStr)
	{
		try
		{
			jsonObj = new JSONObject(jsonStr);
			Iterator<?> iter = jsonObj.keys();
			Object key = null;
			StringBuffer nameSb = new StringBuffer();
			StringBuffer valueSb = new StringBuffer();
			String keyName = "";
			while (iter.hasNext())
			{
				key = iter.next();
				keyName = key.toString();
				nameSb.append(keyName).append("\\.");
				valueSb.append((String) jsonObj.get(keyName)).append(".");
			}
			nameSb.deleteCharAt(nameSb.length() - 1);
			valueSb.deleteCharAt(valueSb.length() - 1);
			pieName = nameSb.toString().split("\\.");
			mValue = transfer(valueSb.toString().split("\\."));
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据数据获取pieName
	 * 
	 * @param jsonStr
	 * @return
	 */
	public String[] getPieName()
	{
		return pieName;
	}
	
	/**
	 * 根据数据获取pieValue
	 * 
	 * @param jsonStr
	 * @return
	 */
	public double[] getPieValue()
	{
		return mValue;
	}
	
	/**
	 * 将double[]转换为Double[]
	 * 
	 * @param mValue
	 * @return
	 */
	public Double[] trans2Double(double[] mValue)
	{
		int mLenth = mValue.length;
		Double[] mData = new Double[mLenth];
		for (int i = 0; i < mLenth; i++)
		{
			mData[i] = Double.valueOf(mValue[i]);
		}
		return mData;
	}
}
